package com.rub.android.jetpack.adapter

import android.graphics.Color
import android.text.TextUtils
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.rub.android.MainActivity
import com.rub.android.R

class ImageAdpter {


    companion object {

        /**
         * itemImage与xml的app:itemImage一致
         * JvmStatic 静态注解
         */
        @JvmStatic
        @BindingAdapter("itemImage")
        fun setImage(img: ImageView, url: String) {

            if (!TextUtils.isEmpty(url)) {
                Glide.with(MainActivity.context)
                    .load(url)
                    //图片加载出来前，显示的图片
                    .placeholder(R.drawable.ic_baseline_preview_24)
                    //图片加载失败后，显示的图片
                    .error(R.drawable.ic_baseline_error_24)
                    .diskCacheStrategy(DiskCacheStrategy.RESOURCE)
                    .into(img)
            } else {
                img.setBackgroundColor(Color.GRAY)
            }

        }
    }
}